package com.hava.homework;

import com.hava.homework.service.FileWriterService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by zhanpeng on 2016/10/14.
 */
public class MainContainer {

    public static void main(String [] args){
        //通过读取Spring配置文件获取ApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("homework-context.xml");

        //由于使用了注解，直接获取Bean对象
        FileWriterService fileWriterService = context.getBean("fileWriterService",FileWriterService.class);
        System.out.println("第一次获取FileWriterService的Bean " + fileWriterService);
        fileWriterService.write("123");
        fileWriterService = context.getBean("fileWriterService",FileWriterService.class);
        System.out.println("第二次获取FileWriterService的Bean " + fileWriterService);
        fileWriterService.write("456");

        //关闭IoC容器
        ((ConfigurableApplicationContext) context).close();
    }
}
